package com.xiaomei.manager.utils;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


/**
* 类名称：DESUtils  
* 创建人：rey  
* 创建时间：2015年6月11日 上午9:40:28  
* 修改人：rey  
* 修改时间：2015年6月11日 上午9:40:28  
* 修改备注：  
* @version   
*
 */
public class DESUtils {
	private static final String Algorithm = "DES"; //定义 加密算法,可用 DES,DESede,Blowfish 
	
	//src为被加密的数据缓冲区（源） 
	public static byte[] encryptMode(byte[] keybyte, byte[] src) { 
		try { 
			//生成密钥 
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
			//加密 
			Cipher c1 = Cipher.getInstance(Algorithm); 
			c1.init(Cipher.ENCRYPT_MODE, deskey); 
			return c1.doFinal(src); 
		} catch (java.lang.Exception e) { 
			e.printStackTrace(); 
		} 
		return null; 
	} 

	//keybyte为加密密钥，长度为24字节 
	//src为加密后的缓冲区 
	public static byte[] decryptMode(byte[] keybyte, byte[] src) { 
		try { 
			//生成密钥 
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
			//解密 
			Cipher c1 = Cipher.getInstance(Algorithm); 
			c1.init(Cipher.DECRYPT_MODE, deskey); 
			return c1.doFinal(src); 
		} catch (java.lang.Exception e) { 
			e.printStackTrace(); 
		} 
		return null; 
	}
	
	//转换成十六进制字符串 
	public static String byte2hex(byte[] b) { 
		String hs = ""; 
		String stmp = ""; 
		for (int n = 0; n < b.length; n++) { 
			stmp = (Integer.toHexString(b[n] & 0XFF)); 
			if (stmp.length() == 1) hs=hs+"0"+stmp; 
			else hs = hs + stmp; 
			if (n < b.length - 1) hs = hs + ""; 
		} 
		return hs.toUpperCase(); 
	} 

	//16 进制 转 2 进制
	public static byte[] hex2byte(String hex) throws IllegalArgumentException {     
	    if (hex.length() % 2 != 0) {     
	        throw new IllegalArgumentException();     
	    }     
	    char[] arr = hex.toCharArray();     
	    byte[] b = new byte[hex.length() / 2];     
	    for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {     
	        String swap = "" + arr[i++] + arr[i];     
	        int byteint = Integer.parseInt(swap, 16) & 0xFF;     
	        b[j] = new Integer(byteint).byteValue();     
	    }     
	    return b;     
	} 
	
	@SuppressWarnings("unused")
	private static byte[] hex2byte(byte[] b) {
		if ((b.length % 2) != 0)
			throw new IllegalArgumentException("长度不是偶数");
		byte[] b2 = new byte[b.length / 2];
		for (int n = 0; n < b.length; n += 2) {
			String item = new String(b, n, 2);
			b2[n / 2] = (byte) Integer.parseInt(item, 16);
		}
		return b2;
	}

	//加密
	@SuppressWarnings("restriction")
	public static String Encrypt(String str, byte[] key){
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		byte[] encrypt = encryptMode(key, str.getBytes());
		return byte2hex(encrypt);
	}
	
	//加密
	@SuppressWarnings("restriction")
	public static byte[] EncryptRetByte(byte[] src, byte[] key){
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		byte[] encrypt = encryptMode(key, src);
		return encrypt;
	}

	//解密
	@SuppressWarnings("restriction")
	public static String Decrypt(String str, byte[] key){
		Security.addProvider(new com.sun.crypto.provider.SunJCE());
		byte[] decrypt = decryptMode(key, hex2byte(str)); 
		return new String(decrypt);
	}
	
	public static void main(String arg[]){
		String strKey = "9694358131015327";
		// 加密jdbc连接
		encryptJdbc(strKey);
		// 加密短信连接
//		encryptJMsg(strKey);
		// 加密sts连接信息
//		encryptSTS(strKey)arg;
	}

	private static void encryptSTS(String strKey) {
		/**
		 * region=cn-hangzhou
		 * sts_api_version=2015-04-01
		 * AccessKeyID=m5YnBWGvC5Z48jmp
		 * AccessKeySecret=YbtxOqLZ1ynzd4imkqj1w2DaFfj8cx
		 * RoleArn=acs:ram::1054644481504230:role/ramappreadonly
		 * TokenExpireTime=900
		 * PolicyFile={  "Statement": [    {      "Action": [        "oss:GetObject",        "oss:PutObject",        "oss:DeleteObject",        "oss:ListParts",        "oss:AbortMultipartUpload",        "oss:ListObjects"      ],      "Effect": "Allow",      "Resource": ["acs:oss:*:*:$BUCKET_NAME/*", "acs:oss:*:*:$BUCKET_NAME"]    }  ],  "Version": "1"}
		*/
		
		String region = "cn-hangzhou";
		String sts_api_version = "2015-04-01";
		String AccessKeyID = "m5YnBWGvC5Z48jmp";
		String AccessKeySecret = "YbtxOqLZ1ynzd4imkqj1w2DaFfj8cx";
		String RoleArn = "acs:ram::1054644481504230:role/xm-chuxing-app";
		String TokenExpireTime = "900";
//		String PolicyFile = "{  \"Statement\": [    {      \"Action\": [        \"oss:GetObject\",        \"oss:PutObject\",        \"oss:DeleteObject\",        \"oss:ListParts\",        \"oss:AbortMultipartUpload\",        \"oss:ListObjects\"      ],      \"Effect\": \"Allow\",      \"Resource\": [\"acs:oss:*:*:$BUCKET_NAME/*\", \"acs:oss:*:*:$BUCKET_NAME\"]    }  ],  \"Version\": \"1\"}";
		String PolicyFile = "{\"Statement\":[{\"Action\": [\"oss:GetObject\",\"oss:PutObject\",\"oss:ListParts\",\"oss:AbortMultipartUpload\",\"oss:ListObjects\"],\"Effect\": \"Allow\",\"Resource\": [\"acs:oss:*:*:xm-chuxing-app/*\",\"acs:oss:*:*:xm-chuxing-app\"]}],\"Version\": \"1\"}";
		PolicyFile = PolicyFile.replaceAll(" ", "");
		
		region = Encrypt(region, hex2byte(strKey));
		sts_api_version = Encrypt(sts_api_version, hex2byte(strKey));
		AccessKeyID = Encrypt(AccessKeyID, hex2byte(strKey));
		AccessKeySecret = Encrypt(AccessKeySecret, hex2byte(strKey));
		RoleArn = Encrypt(RoleArn, hex2byte(strKey));
		TokenExpireTime = Encrypt(TokenExpireTime, hex2byte(strKey));
		PolicyFile = Encrypt(PolicyFile, hex2byte(strKey));
		
		System.out.println(region);
		System.out.println(sts_api_version);
		System.out.println(AccessKeyID);
		System.out.println(AccessKeySecret);
		System.out.println(RoleArn);
		System.out.println(TokenExpireTime);
		System.out.println(PolicyFile);
		
		region = Decrypt(region, hex2byte(strKey));
		sts_api_version = Decrypt(sts_api_version, hex2byte(strKey));
		AccessKeyID = Decrypt(AccessKeyID, hex2byte(strKey));
		AccessKeySecret = Decrypt(AccessKeySecret, hex2byte(strKey));
		RoleArn = Decrypt(RoleArn, hex2byte(strKey));
		TokenExpireTime = Decrypt(TokenExpireTime, hex2byte(strKey));
		PolicyFile = Decrypt(PolicyFile, hex2byte(strKey));
		
		
		System.out.println(region);
		System.out.println(sts_api_version);
		System.out.println(AccessKeyID);
		System.out.println(AccessKeySecret);
		System.out.println(RoleArn);
		System.out.println(TokenExpireTime);
		System.out.println(PolicyFile);
	}
	
	private static void encryptJdbc(String strKey) {
		/**
		 * jdbc_driverClassName=com.mysql.jdbc.Driver
		 * jdbc_url=jdbc:mysql://localhost:3306/xfn100?useUnicode=true&amp;characterEncoding=utf-8
		 * jdbc_username=root
		 * jdbc_password=fc2014
		 */
		

		String jdbc_url = "jdbc:mysql://localhost:3306/xfn100?useUnicode=true&amp;characterEncoding=utf-8";
		String jdbc_username = "root";
		String jdbc_password = "rootroot";
//		String jdbc_url = "jdbc:mysql://rm-2zeu777997jrz71zko.mysql.rds.aliyuncs.com:3306/xiaomei?useUnicode=true&characterEncoding=utf-8";
//		String jdbc_username = "xiaomei";
//		String jdbc_password = "xiaomei@chuxing123";
		
		jdbc_url = Encrypt(jdbc_url, hex2byte(strKey));
		jdbc_username = Encrypt(jdbc_username, hex2byte(strKey));
		jdbc_password = Encrypt(jdbc_password, hex2byte(strKey));
		
		System.out.println(jdbc_url);
		System.out.println(jdbc_username);
		System.out.println(jdbc_password);
		
		jdbc_url = Decrypt(jdbc_url, hex2byte(strKey));
		jdbc_username = Decrypt(jdbc_username, hex2byte(strKey));
		jdbc_password = Decrypt(jdbc_password, hex2byte(strKey));
		
		
		System.out.println(jdbc_url);
		System.out.println(jdbc_username);
		System.out.println(jdbc_password);
	}
	
	private static void encryptJMsg(String strKey) {
		
		String msg_url = "http://222.73.117.158/msg/";
		String msg_account = "yanqiuping_hp";
		String msg_pswd = "Hp888888";
		String msg_needstatus = "false";
		
		msg_url = Encrypt(msg_url, hex2byte(strKey));
		msg_account = Encrypt(msg_account, hex2byte(strKey));
		msg_pswd = Encrypt(msg_pswd, hex2byte(strKey));
		msg_needstatus = Encrypt(msg_needstatus, hex2byte(strKey));
		
		System.out.println(msg_url);
		System.out.println(msg_account);
		System.out.println(msg_pswd);
		System.out.println(msg_needstatus);
		
		msg_url = Decrypt(msg_url, hex2byte(strKey));
		msg_account = Decrypt(msg_account, hex2byte(strKey));
		msg_pswd = Decrypt(msg_pswd, hex2byte(strKey));
		msg_needstatus = Decrypt(msg_needstatus, hex2byte(strKey));
		
		
		System.out.println(msg_url);
		System.out.println(msg_account);
		System.out.println(msg_pswd);
		System.out.println(msg_needstatus);
	}
}